package com.example.idea.service;

import cn.hutool.core.util.IdUtil;
import com.example.idea.entity.Orders;
import com.example.idea.entity.User;
import com.example.idea.mapper.OrdersMapper;
import com.example.idea.utils.Page;
import com.example.idea.utils.TokenUtils;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class OrdersService {

    @Resource
    private OrdersMapper ordersMapper;

    public void add(Orders orders) {
        orders.setDate(new java.text.SimpleDateFormat("yyyy-MM-dd").format(new java.util.Date()));
        User currentUser = TokenUtils.getCurrentUser();
        orders.setUserid(currentUser != null ? currentUser.getId() : 0); // 0 或其他默认值
        orders.setNo(IdUtil.fastSimpleUUID());
        ordersMapper.add(orders);
    }

    public void update(Orders orders) {
        ordersMapper.update(orders);
    }

    public void delete(Integer id) {
        ordersMapper.delete(id);
    }

    public Orders query(Integer id) {
        return ordersMapper.query(id);
    }

    public void deleteBatch(List<Integer> ids) {
        String idsStr = ids.toString().replace("[", "").replace("]", "");
        ordersMapper.deleteBatch(idsStr);
    }

    public List<Orders> list() {
        return ordersMapper.list();
    }

    public List<Orders> selectList(Orders orders) {
        return ordersMapper.selectList(orders);
    }

    public Page<Orders> selectByPage(Integer pageNum, Integer pageSize, String category, String name) {
        Integer skipNum = (pageNum - 1) * pageSize;
        Page<Orders> page = new Page<>();
        List<Orders> logsList = ordersMapper.selectByPage(skipNum, pageSize, category, name);
        Integer total = ordersMapper.selectCountByPage(category, name);
        page.setList(logsList);
        page.setTotal(total);
        return page;
    }
}
